package com.xasz.controller;

import com.xasz.entity.User;
import com.xasz.service.IUserService;
import com.xasz.util.JsonResult;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cai
 * @since 2022-08-02
 */
@AllArgsConstructor
@RestController
@RequestMapping("user")
public class UserController extends BaseController{
    private IUserService userService;

    /**
     * 登录
     */
    @GetMapping("/login")
    public  JsonResult<User>  login(@RequestBody User users,HttpSession session){
        User user=userService.login(users.getUsername(),users.getPassword());
        session.setAttribute("uid",user.getId());
        session.setAttribute("username",user.getUsername());
        return new JsonResult<>(OK);
    }
    /**
     * 注册
     */
    @PostMapping("/reg")
    public  JsonResult<Void>  reg(@RequestBody User users){
        userService.reg(users.getUsername(),users.getPassword());
        return new JsonResult<>(OK);
    }
    /**
     * 登出
     */
    @DeleteMapping
    public JsonResult<Void> outLogin(HttpSession session){
        session.invalidate();
        return new JsonResult<>(OK);
    }
}
